from diff import *

#################以下研究的情况皆是ETF滞后于stock：
e = abs(diffETF[78]-diff1[78])
#print(e)
f = abs(diffETF[20]-diff1[20])
#print(f)

#判断点位是波峰  还是不是波峰
peak = True
antipeak= False
def judge(i,data):
     if i < len(data) - 1 and data[i]> data[i+1] and data[i]*data[i+1]<0:
        return True
     else:
        return False

#求出滞后点

def getpeak(data1,data2,):
    point1 = []
    for i in range(len(data1)-1):
        a = judge(i,data1)
        b = judge(i,data2)
        c = abs((data1[i] - data2[i]))
        if a ==True and b ==False:
            if c <=0.10701970301:
                point1.append(i)
    return point1

a = getpeak(diff1,diffETF)
#print(a)



#我怎么写了俩函数啊我铐，什么脑残，我看不懂的啊
trough = True
antitrough = False
def judge1(i,data):
    if i < len(data) -1 and data[i] < data[i+1] and data[i]*data[i+1] < 0:
        return True
    else:
        return False

def gettrough(data1,data2):
    point2 = []
    for i in range(len(data1) - 1):
        a = judge1(i,data1)
        b = judge1(i,data2)
        c = abs(data1[i] - data2[i])
        if a ==True and b ==False:
            if c <=0.27515170876399997:
                point2.append(i)
    return point2
b = gettrough(diff1,diffETF)
#print(b)







shares={'ETF':0,'stock':12608,'cash':0}#同时持有仨，ETF stock对应的是份额
shares['stock']=int(500000/stockprice[0]) #先持有股票份额为
#print(shares['stock'])
#shares['ETF'] = int(500000/ETFprice[0])
#print(shares['ETF'])
value = []#要算的贴现价值


for i in range(len(ETFprice)):
    a = judge(i, diff1)#判断stock是否为波峰
    b = judge(i, diffETF)#判断ETF是否为波峰
    c = judge1(i,diff1)#判断stock是否为波谷
    d = judge1(i,diffETF)#判断ETF是否为波谷
    f = abs(diff1[i] - diffETF[i])

    if shares['stock'] > 0:
        if a == True and b == False:
            if f <= 0.10701970301:
                shares['ETF'] = (stockprice[i] * shares['stock'] / ETFprice[i])*0.9999
                shares['stock'] = 0

    elif shares['ETF'] > 0:
        if b ==True and a == False:
            if f <= 0.10701970301:
                shares['stock'] = (ETFprice[i]*shares['ETF']/stockprice[i])*0.9999
                shares['ETF'] = 0
    val = shares['ETF'] * ETFprice[i] + shares['stock'] * stockprice[i]
    value.append(val)



import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(dates,value)
plt.plot(dates,stockprice*12608)
plt.plot(dates,ETFprice*554938)


maxfallback = (value[0]-min(value))/value[0]
print(maxfallback)


import math
rateofreturn = []
for i in range(1,len(value)):
    a = (value[i]-value[0])/value[0]
    rateofreturn.append(a)

rateofreturn1 = [math.nan] + rateofreturn
print(rateofreturn1)
fig1 = plt.figure()
plt.plot(dates,rateofreturn1)
plt.show()

import numpy as np


# 计算几何平均收益率
geometric_mean_return = np.prod(1 + np.array(rateofreturn)) ** (1 / len(rateofreturn)) - 1
# 计算收益率标准差
returns_mean = np.mean(rateofreturn)
returns_std = np.std(rateofreturn)

print("几何平均收益率:", geometric_mean_return)
print("收益率标准差:", returns_std)
















